package com.hu.bgp.client;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.inject.Inject;
import com.hu.bgp.client.gin.BgpGinModule;
import com.hu.bgp.client.gin.BgpGinjector;
import com.hu.bgp.client.localevent.PlaceChangedEvent;
import com.hu.bgp.client.localevent.PlaceChangedEventHandler;
import com.hu.bgp.client.place.Place;
import com.hu.bgp.client.place.PlaceService;
import com.hu.bgp.client.presenter.Boardgame;
import com.hu.bgp.client.presenter.BoardgamePlace;
import com.hu.bgp.client.presenter.GameSessionPlace;
import com.hu.bgp.client.presenter.GameSessions;
import com.hu.bgp.client.presenter.GameTypePlace;
import com.hu.bgp.client.presenter.GameTypes;
import com.hu.bgp.client.presenter.PlacePresenter;
import com.hu.bgp.client.presenter.Presenter;
import com.hu.bgp.client.rpc.BgpService;
import com.hu.bgp.client.rpc.BgpServiceAsync;
import com.hu.bgp.framework.clientapi.util.Firebug;

public class AppController implements Presenter
{
	private PlaceService placeService;
	private final EventBus eventBus;
	private final BgpServiceAsync rpcService;
	private static HasWidgets container;
	private HasWidgets content;
	private final BgpGinjector bgpInject = GWT.create(BgpGinjector.class);
	

	@Inject
	public AppController() {
		this.eventBus = bgpInject.getEventBus();
		this.rpcService = bgpInject.getBgpService();
		placeService = new PlaceService(eventBus);
		
		bind();
	}

	private void bind() {
		
		eventBus.addHandler(PlaceChangedEvent.TYPE, new PlaceChangedEventHandler(){

			public void changePlaceTo(Place place) {
				if(place instanceof GameTypePlace){
					GameTypes p = bgpInject.getGameTypeWidget();				
					p.go(container);
				}
				if(place instanceof BoardgamePlace)
					{
					Boardgame p = bgpInject.getBoardGameWidget();
					p.setPlace((BoardgamePlace) place);
					p.go(container);
					}
				if(place instanceof GameSessionPlace){
					GameSessions p = bgpInject.getGameSessionsWidget();
					p.setPlace((GameSessionPlace) place);
					p.go(container);
				}
				
			}
			
		});
		

	}

	public void go(final HasWidgets container) {
		this.container = container;

		if ("".equals(History.getToken())) {
			History.newItem("gametypes");
		} else {
			History.fireCurrentHistoryState();
		}
	}


	
}
